package com.theoryinpractice.hudson;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.theoryinpractice.hudson.ui.BrowserPanel;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: May 16, 2007
 * Time: 8:48:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class HudsonComponent implements ProjectComponent {

    private Project project;
    private ToolWindow toolWindow;
    private BrowserPanel browserPanel;

    private final String TOOL_WINDOW_ID = "Hudson";

    public HudsonComponent(Project project) {
        this.project = project;
    }

    public void projectOpened() {

        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);

        if (toolWindow == null) {
            if (browserPanel == null) {
                browserPanel = new BrowserPanel(project);
            }


            toolWindow = toolWindowManager.
                    registerToolWindow(TOOL_WINDOW_ID, browserPanel.getPanel(), ToolWindowAnchor.RIGHT);

//                    toolWindow.setIcon(IconLoader.getIcon("/hudson.gif", HudsonComponent.class));

//                    jiraMonitor.setToolWindow(toolWindow);
        }

    }

    public void projectClosed() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        if (toolWindowManager.getToolWindow(TOOL_WINDOW_ID) != null) {
            toolWindowManager.unregisterToolWindow(TOOL_WINDOW_ID);
            toolWindow = null;
        }
    }

    @NonNls
    @NotNull
    public String getComponentName() {
        return "Hudson";  //To change body of implemented methods use File | Settings | File Templates.
    }


    public void initComponent() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void disposeComponent() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

